package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.ModuleDao;
import com.itheima.domain.system.Module;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.ModuleService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ModuleServiceImpl implements ModuleService {

    @Override
    public void save(Module module) {
        SqlSession sqlSession = null;
        try{
            // 1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            // 2. 获取Dao
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession,ModuleDao.class);

//            id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            module.setId(id);

            // 3.调用Dao层操作(这个module页面上不可能然你手动填，所以使用UUID生成策略获取 )
            moduleDao.save(module);
            // 4.提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
//            出现问题的话要回滚
            TransactionUtil.rollback(sqlSession);
//            不能直接throw e 因为他不是那种运行时的异常，给他包装一下，他整个过程就不需要你管，就会直接往出抛，抛到最上面那一层
            throw new RuntimeException(e);
//            记录日志
        }finally{
//            关闭SqlSession前要处理一下，如果是空的话关不了怎么办
            try{
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void delete(Module module) {

        SqlSession sqlSession = null;

        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            moduleDao.delete(module);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
//          记录日志

        }finally {
            try{
                TransactionUtil.close(sqlSession);
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void update(Module module) {

        SqlSession sqlSession = null;

        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            moduleDao.update(module);
            TransactionUtil.commit(sqlSession);
        }catch(Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
//            记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public Module findById(String id) {

        SqlSession sqlSession = null;

        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findById(id);
//            查询操作可以没有事务
//            TransactionUtil.commit(sqlSession);
        }catch(Exception e){
//            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
//            记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Module> findAll() {

        SqlSession sqlSession = null;
        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findAll();
        }catch(Exception e){
            throw new RuntimeException(e);
//            记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            PageHelper.startPage(page,size); // 这句话必须出现在查询的前面
            List<Module> all = moduleDao.findAll();
            PageInfo pageInfo = new PageInfo(all);

            return pageInfo;

        }catch(Exception e){
            throw new RuntimeException(e);
//            记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Map> findAuthorDataByRoleId(String roleId) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao moduleDao = MapperFactory.getMapper(sqlSession, ModuleDao.class);
            return moduleDao.findAuthorDataByRoleId(roleId);
        }catch(Exception e){
            throw new RuntimeException(e);
//            记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
